<?php 
require_once("../Const.php");

  try {
  	$dataName = "pdo_db";
  	$host = host;
  	//dsn必须指明连接的是哪个数据库，不管这个数据库有没有创建
  	$dsn = "mysql:host=$host;dbname=$dataName";//数据源名称或叫做  

  	$pdo = new PDO($dsn,mysql_username,mysql_password);

  	//setAttribute设置属性
  	//设置错误抛出的方式为Exception
    $pdo -> setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
    //对于数据库中null和空串的处理，可以设置成转换为空串PDO::NULL_TO_STRING
    $pdo -> setAttribute(PDO::ATTR_ORACLE_NULLS,PDO::NULL_NATURAL);
    
    $pdo -> exec("SET NAMES 'utf8'");//编码


    $cDB = "create database if not exists $dataName character set gbk";//
   
    $pdo -> exec($cDB);

    $cTable = "create table if not exists MyGuests (mark int unsigned auto_increment primary key,
     firstName varchar(30) not null,
     lastName varchar(30) not null,
     email varchar(50),
     reg_date timestamp
    )";
    $pdo -> exec($cTable);//执行前要先指明数据库，在dsn中
  	
    
    //预编译语句也可以用“？”，PDO中可以用两种形式，mysqli只能用一种
    $insetSql = "insert into MyGuests (firstName,lastName,email) values (:firstName,:lastName,:email)";
    //返回一个预处理对象
    $pdoStmt = $pdo -> prepare($insetSql);
    var_dump($pdoStmt);

    //PDO::PARAM_INT int类型
    $pdoStmt -> bindParam(":firstName",$firstName,PDO::PARAM_STR);
    $pdoStmt -> bindParam(":lastName",$lastName,PDO::PARAM_STR);
    $pdoStmt -> bindParam(":email",$email,PDO::PARAM_STR);

    $firstName = random_int(10,3000)."璐姐";
    $lastName = random_int(90,1000)."佳姐";
    $email = "000123.@sina.com";
    $pdoStmt -> execute();

    
    $firstName = random_int(10,3000)."璐姐";
    $lastName = random_int(90,1000)."佳姐";
    $email = "000123.@sina.com";
    $pdoStmt -> execute();

  	
  } catch (PDOException $e) {
  	
  	die("pdo连接失败".$e->getMessage());
  };
   $pdo=null;//断开数据库连接
 ?>